$(document).ready(function () {
    get_captcha();

    $("#code").on("click", function () {
        get_captcha();
    });

    $("#login-submit").on("click", function () {
        $.ajax({
            url: "/user/login",
            method: "post",
            data: $("#login-form").serialize()
        }).then(function (data) {
                var status = $(data).find("error_code").text();
                switch (status) {
                    case "0":
                        window.location.href = "/pages/index.html";
                        break;
                
                    default:
                        alert("Wrong user name or password!")
                        break;
                }
            });
    });

});

function get_captcha() {
    $.ajax({
        url: "/user/captcha?captcha=",
        method: "get",
    }).then(function (data) {
        $("#code").val(data);
    });
}